<?php


session_start();

if (!empty($_SESSION['login'])&&!empty($_SESSION['email'])){

require_once('../config.inc.php');
require_once('../../classes/Mypdo.class.php');
require_once('../../classes/ProprietaireManager.class.php');
require_once('../../classes/PersonneManager.class.php');
require_once('../../classes/LocationManager.class.php');
require_once('../../classes/ChargeManager.class.php');
require_once('../../classes/Proprietaire.class.php');
require_once('../../classes/Personne.class.php');
require_once('../../classes/Location.class.php');
require_once('../../classes/LocationRegle.class.php');
require_once('../../classes/Charge.class.php');
require_once('../../classes/Adresse.class.php');
require_once('../../classes/ImmeubleManager.class.php');
require_once('../../classes/Immeuble.class.php');

$db = new Mypdo();
define('FPDF_FONTPATH',$_SERVER['DOCUMENT_ROOT'].'/image/fonts/');
require('../../classes/Fpdf.class.php');
if(!empty($_POST['date_loyer'])&&!empty($_POST['id_loc'])&&!empty($_POST['id_pers'])){

$_SESSION['id_loc'] = $_POST['id_loc'];
$_SESSION['id_pers'] = $_POST['id_pers'];
$_SESSION['date_loyer'] = $_POST['date_loyer'];

         //         var_dump($_POST);
$propriManager = new ProprietaireManager($db);

//Proprietaire
$id_propri = $propriManager->getIdPropriByIdLoc($_SESSION['id_loc']);
$propri = $propriManager->getPropri($id_propri);
$persManager = new PersonneManager($db);

//Personne
$pers = $persManager->getPers($_SESSION['id_pers']);

//Date du jour
$now = new DateTime('now');

//date du loyer
$date_loyer = new DateTime($_SESSION['date_loyer']);
$date_loyer = $date_loyer->format('d/m/Y');
$mois = array('01'=>'Janvier','02'=>'Fevrier','03'=>'Mars','04'=>'Avril','05'=>'Mai','06'=>'Juin','07'=>'Juillet','08'=>'Aout','09'=>'Septembre','10'=>'Octobre','11'=>'Novembre','12'=>'Decembre');

$locManager = new LocationManager($db);

//Location
$loc = $locManager->getLoc($_SESSION['id_loc']);

//Immeuble
$immeuManager = new ImmeubleManager($db);
$immeu = $immeuManager->getImmeu($loc->getIdImmeu());

$chargeManager = new ChargeManager($db);

//Ensemble des charges
$listeCharge = $chargeManager->getListByIdLoc($_SESSION['id_loc']);

$brutAPayer = $loc->getMontantLoc();

$netAPayer = $brutAPayer + $brutAPayer*($loc->getTvaLoc()/100);

//Montant du loyer
$montant_loyer = $netAPayer;



$adressePropri = $propri->getAdressePropri();
$nomPropri = $propri->getNomPropri().' '.$propri->getPrenomPropri();
$ad_rue = $adressePropri->getNumAdresse().' '.$adressePropri->getVoieAdresse();
$ad_ville = $adressePropri->getCpAdresse().' '.$adressePropri->getVilleAdresse();

$adressePers = $pers->getAdressePers();
$dest = $pers->getGenrePers().' '.$pers->getNomPers().' '.$pers->getPrenomPers()."\n".
    $adressePers->getNumAdresse().' '.$adressePers->getVoieAdresse()."\n".
    $adressePers->getCpAdresse().' '.$adressePers->getVilleAdresse();

$siret = 'Siret : '.$propri->getSiretPropri();
$ape = 'Ape : '.$propri->getApePropri();

$titre = 'Relance';

$txtd = 'Malgré nos différentes relances, votre compte laisse apparaître un solde débiteur.';
$date = $mois[$now->format('m')].' '.$now->format('Y');

    $txt = 'Veuillez nous faire parvenir votre règlement de ';
    $txt2 = ' par retour de courrier. ';

    $txt4 = "\n".'Sans règlement de votre part sous 48 heures, nous prendrons les mesures nécessaires
prévues par la loi.';

    if($_POST['choix'] == 2){
        $txt2 =  $txt2.$txt4;
    }

$adresseImmeu = $immeu->getAdresseImmeu();
$immeu_ad_rue = $adresseImmeu->getNumAdresse().' '.$adresseImmeu->getVoieAdresse();
$immeu_ad_ville = $adresseImmeu->getCpAdresse().' '.$adresseImmeu->getVilleAdresse();

$date_jour = 'Le '.$now->format('d').' '.$mois[$now->format('m')].' '.$now->format('Y').'.';

$nbFormat = new NumberFormatter('fr_FR',NumberFormatter::CURRENCY);
$nbFormat->setSymbol(NumberFormatter::CURRENCY_SYMBOL,'');

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','',12);
$pdf->SetMargins(25,30);
$pdf->Ln(1);
$pdf->Cell(50,10,utf8_decode($nomPropri));
$pdf->Ln(6);
$pdf->Cell(40,10,utf8_decode($ad_rue));
$pdf->Ln(6);
$pdf->Cell(40,10,utf8_decode($ad_ville));
$pdf->Ln(10);
$pdf->setX(120);
$pdf->MultiCell(70,10,utf8_decode($dest));
$pdf->Ln(1);
$pdf->Cell(50,10,utf8_decode($siret));
$pdf->Ln(6);
$pdf->Cell(50,10,utf8_decode($ape));
$pdf->Ln(15);

$w = $pdf->GetStringWidth($titre)+6;
$pdf->SetX((210-$w)/2);
$pdf->Cell($w,10,utf8_decode($titre));
$pdf->Ln(20);


$pdf->MultiCell(150,6,utf8_decode($txtd));
    $pdf->SetX(60);

    $pdf->Cell(30,10,utf8_decode($date));

//$pdf->Ln(6);
//$pdf->Cell(40,10,utf8_decode($ad_rue));
//$pdf->Ln(6);
//$pdf->Cell(40,10,utf8_decode($ad_ville));
$pdf->Ln(16);

$pdf->SetX(60);
$pdf->Cell(5,5,'Loyer');

$pdf->Cell(85,5,utf8_decode($nbFormat->format($montant_loyer)).chr(128),0,0,'R');
foreach ($listeCharge as $value) {

    $montant_charge = $value->getMontantCharge()+$value->getMontantCharge()*($value->getTvaCharge()/100);

    $pdf->Ln(5);
    $pdf->SetX(60);
    $wnom = $pdf->GetStringWidth($value->getNomCharge());
    $pdf->Cell($wnom,5,utf8_decode($value->getNomCharge()));
    $pdf->Cell(90-$wnom,5,utf8_decode($nbFormat->format($montant_charge)).chr(128),0,0,'R');
}
//Net a Payer
foreach ($listeCharge as $value) {
    $montant = $value->getMontantCharge();

    $tva =  ($value->getTvaCharge()/100);
    $charge = ($montant + ($montant * $tva ));
    $netAPayer = $netAPayer+ $charge;
}
$pdf->Ln(20);
$pdf->MultiCell(200,10,utf8_decode($txt.$nbFormat->format($netAPayer)).chr(128).utf8_decode($txt2));


$txt3 = 'Veuillez agréer l\'expression de nos salutations distinguées.';
$pdf->Ln(5);
$pdf->Cell(150,30,utf8_decode($txt3));
$pdf->Ln(25);
$pdf->SetX(150);

$pdf->Cell(50,10,utf8_decode($date_jour));
$pdf->Ln(5);
$pdf->SetX(150);
$pdf->Cell(50,10,utf8_decode($nomPropri));

$pdf->SetFillColor(255);
$pdf->RoundedRect(110, 32, 80, 30, 5, 'D', '1234');
$pdf->RoundedRect(40, 84, 120, 10, 5, 'D', '1234');
$pdf->RoundedRect(55, 130, 100, 15 + 5*count($listeCharge), 5, 'D', '1234');

$pdf->Output();

}else{
   // header('Location: ../../index.php?page=203');
    var_dump($_POST);
}
}else{
    header('Location: index.php');
}